<template>
  <div class="my-tab-bar">
    <!-- 根据接受的数组生成底部，添加点击事件，让activeIndex等于当前点击的索引，
    并且把当前项的组件名传递父组件，以实现切换动态组件功能添加动态类名，
    通过判断索引是否等于activeIndex决定是否显示这个样式 -->
  	<div class="tab-item" 
    v-for="(item,index) in list" 
    :key="index" 
    :class="{current : index=== activeIndex}" 
    @click="clickFn(index,item.componentName)" >
      <!-- 图标 -->
      <span class="iconfont" :class="item.iconText"></span>
      <!-- 文字 -->
      <span>{{item.text}}</span>
    </div>
  </div>
</template>

<script>
export default {
  // 接受父组件传递的数据，并且只接受2-5个元素的数组
  props: {
    list : {
      type : Array,
      required : true,
      validator(val){
        if(val.length<2||val.length>=5){
          console.error("prop list must between 2 and 5");
          return false;
      }
      return true;
    }
  }
  },
  methods: {
    clickFn(index,componentName){
      this.activeIndex = index;
      this.$emit("changeCom",componentName)
    }
  },
  data () {
    return {
      activeIndex : 0,
    }
  }
  }
</script>

<style lang="less" scoped>
.my-tab-bar {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 50px;
  border-top: 1px solid #ccc;
  display: flex;
  justify-content: space-around;
  align-items: center;
  background-color: white;
  .tab-item {
    display: flex;
    flex-direction: column;
    align-items: center;
  }
}
    
.current {
  color: #1d7bff;
}
</style>